package com.gccloud.design;

import com.gccloud.design.dto.SdRepoParticipantConfDto;
import com.gccloud.design.service.SdParticipantConfService;
import com.gccloud.design.vo.SdRepoParticipantConfVo;
import com.gccloud.starter.common.exception.GlobalException;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.mybatis.page.PageVO;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 冯海琴
 */
@RestController
@RequestMapping("/linkParticipantConfig")
@Api(tags = "环节参与者配置API")
@ApiSort(value = 13)
public class SdRepoParticipantConfController {

    private static final Logger log = LoggerFactory.getLogger(SdRepoParticipantConfController.class);

    @Resource
    private SdParticipantConfService sdParticipantConfService;

    /**
     * 根据环节id，分页查询环节参与者
     */
    @ApiOperation(value = "根据环节id，分页查询环节参与者", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/getParticipantByRepoId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "query", required = true, dataType = "long"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", required = true, dataType = "long"),
            @ApiImplicitParam(name = "repoId", value = "查询条件，环节id", paramType = "query", dataType = "string")
    })
    public R<Map<String, Object>> getParticipantByRepoId(String repoId, String activityName, PageVO pageObj) {
        log.info("========================后台接收的数据为:repoId {}========================" + repoId);
        if (StringUtils.isEmpty(repoId)) {
            throw new GlobalException("请求入参有误");
        } else {
            return R.success(sdParticipantConfService.getParticipantByRepoId(repoId, activityName, pageObj));
        }
    }

    /**
     * 根据环节id，查询环节参与者
     */
    @ApiOperation(value = "查询环节参与者", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/getParticipantListByRepoId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "repoId", value = "环节id", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "activityName", value = "环节名称", paramType = "query", dataType = "string")
    })
    public R getParticipantListByRepoId(String repoId, String activityName) {
        log.info("========================后台接收的数据为:repoId {}========================" + repoId);
        if (StringUtils.isEmpty(repoId) || StringUtils.isEmpty(activityName)) {
            throw new GlobalException("请求入参有误");
        } else {
            return R.success(sdParticipantConfService.getParticipantListByRepoId(repoId, activityName));
        }
    }

    /**
     * 添加环节参与者
     */
    @ApiOperation(value = "保存环节参与者")
    @PostMapping(value = "/saveParticipant", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Object> saveParticipant(@RequestBody List<SdRepoParticipantConfDto> sdRepoParticipantConfDtos) {
        log.info("========================后台接收的数据为" + sdRepoParticipantConfDtos + " ========================");
        if (sdRepoParticipantConfDtos == null) {
            throw new GlobalException("请求入参有误");
        } else {
            boolean isSave = sdParticipantConfService.saveParticipant(sdRepoParticipantConfDtos);
            if (isSave) {
                return R.success("保存环节参与者成功");
            } else {
                throw new GlobalException("保存环节参与者失败");
            }
        }
    }

    /**
     * 修改环节参与者
     */
    @PostMapping(value = "/updateParticipant")
    @ApiOperation(value = "修改环节参与者")
    public R<Object> newModifyButton(@RequestBody SdRepoParticipantConfDto sdRepoParticipantConfDto) {
        log.info("========================后台接收的数据 linkParticipantConfigJson{}========================" + sdRepoParticipantConfDto.toString());
        if (sdRepoParticipantConfDto.getId() == null || sdRepoParticipantConfDto.getId() == 0) {
            throw new GlobalException("请求入参有误");
        } else {
            return R.success(sdParticipantConfService.updateParticipant(sdRepoParticipantConfDto));
        }
    }

    /**
     * 删除环节参与者
     */
    @ApiOperation(value = "删除环节参与者")
    @GetMapping(value = "/removeParticipantConfByParticipantConfId", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Object> removeParticipantConfByParticipantConfId(Integer participantConfId) {
        log.info("========================后台接收的数据 participantConfId ========================" + participantConfId);
        if (participantConfId == null || participantConfId == 0) {
            throw new GlobalException("请求入参有误");
        } else {
            sdParticipantConfService.removeParticipantConfByParticipantConfId(participantConfId);
            return R.success();
        }
    }


    /**
     * 根据环节id，查询环节参与者,到人
     */
    @ApiOperation(value = "查询环节参与者,到人", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/getStaffListByRepoId")
    @ApiImplicitParam(name = "repoId", value = "查询条件", paramType = "query", dataType = "string")
    public R<List<SdRepoParticipantConfVo>> getStaffListByRepoId(String repoId, String activityName) {
        log.info("查询环节参与者repoId: {}" + repoId);
        if (StringUtils.isEmpty(repoId)) {
            throw new GlobalException("请求入参有误");
        } else {
            return R.success(sdParticipantConfService.getStaffListByRepoId(repoId, activityName));
        }
    }
}
